<?php
namespace app\Activity\controller;
use app\activity\model\Activity as ActivityModel;
use app\activity\model\ActivityItemRecord as ActivityItemRecordModel;
use app\activity\model\ActivityItemsInActivity as ActivityItemsInActivityModel;
use app\activity\model\ActivityUserRecord as ActivityUserRecordModel;
use app\AdminRest;
use longbingcore\wxcore\Excel;
use think\App;
use think\facade\Db;
use think\Request;

class AdminExcel extends AdminRest
{

    protected $order_model;




    /**
     * @author chenniang
     * @DataTime: 2020-06-18 09:59
     * @功能说明:报名内容导出
     */

    public function userRecordExcel(){


        $param = $this->_param;

        if (!isset($param['activity_id']))
        {
            return $this->error("activity_id is required");
        }
        $activity_id = $param['activity_id'];

        $activity_user_records = [];

        $filter['uniacid'] = $this->_uniacid;

        $filter['activity_id'] = $activity_id;

        if(isset($param['is_sign_in'])&&$param['is_sign_in']!=3){

            $filter['is_sign_in'] = $param['is_sign_in'];
        }

        $where = [];

        //时间筛选
        if(!empty($param['start_time'])&&!empty($param['end_time'])){

            $start_time = $param['start_time'];

            $end_time   = $param['end_time']+86399;

            $where[] = ['create_time','between',"$start_time,$end_time"];
        }


        if(!empty($param['id'])){

            $where[] = ['id','in',$param['id']];
        }

        $model = new ActivityUserRecordModel();

        $records_tmp = $model->getList($filter,$where);

        foreach ($records_tmp as $record) {

            $activity_user_records[$record['user_id']] =
                [
                    'user_id'     => $record['user_id'],
                    'create_time' => $record['create_time'],
                    'nickName'    => !empty($record['userInfo']['nickName'])?$record['userInfo']['nickName']:'',
                    'is_sign_in'  => $record['is_sign_in']==1?'已签到':'未签到',
                    'id'          => $record['id']
                ];
        }

        $m_item_record = new ActivityItemRecordModel();

        $record_items  = $m_item_record->getListByUserIdsAndActivityId(array_keys($activity_user_records), $activity_id);

        $header_data   = $this->getTableHead($activity_id);

        $header        = array_column($header_data,'label');

        $new_data      = [];

        if(!empty($record_items)){

            foreach ($record_items as $item) {

                $activity_user_records[$item['user_id']][crc32($item['title'])] = $item['content'];

            }
            //表头
            $header_icon = array_column($header_data,'prop');


            foreach ($activity_user_records as $k=>$v){

                $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);

                $info   = array();

                foreach ($header_icon as $vs){

                    $info[] = array_key_exists($vs,$v)?$v[$vs]:'';
                }
                $new_data[] = $info;
            }
        }

        $model = new ActivityModel();

        $active_name = $model->where(['id'=>$activity_id])->value('title');

        $name = $active_name.'-报名列表';

        $excel_class = new Excel();

        $result = $excel_class->excelExport($name,$header,$new_data);

        return $this->success($result);


    }


    /**
     * @author chenniang
     * @DataTime: 2020-06-18 10:14
     * @功能说明:获取表头
     */

    public function getTableHead($activity_id)
    {

        //准备表头
        $m_activity_item_in_activity = new ActivityItemsInActivityModel();

        $items_in_activity = $m_activity_item_in_activity->getListByFilter(['activity_id' => $activity_id]);

        $table_head = [
            ['label' => '微信昵称', 'prop' => 'nickName', 'type' => 'txt'],

//            ['label' => '微信头像', 'prop' => 'avatarUrl', 'type' => 'img'],

            ['label' => '报名时间', 'prop' => 'create_time', 'type' => 'timestamp'],

            ['label' => '签到状态', 'prop' => 'is_sign_in', 'type' => 'txt'],
        ];

        foreach ($items_in_activity as $item) {

            $table_head[] = ['label' => $item['title'], 'prop' => (string)crc32($item['title']), 'type' => 'txt'];
        }

//        $result = array_column($table_head,'label');

        return $table_head;

    }

}
